home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ProgressPanel.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  160 lines

  1. /*
  2.  * @(#)ProgressPanel.java    1.9 98/04/10
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.text.*;
  23. import com.sun.java.accessibility.*;
  24.  
  25. import java.awt.*;
  26. import java.awt.event.*;
  27. import java.util.*;
  28.  
  29.  
  30. /**
  31.  * Demo the Progress Bar
  32.  *
  33.  * @version 1.9 04/10/98
  34.  * @author Jeff Dinkins
  35.  # @author Peter Korn (accessibility support)
  36.  */
  37. public class ProgressPanel extends JPanel implements ActionListener {
  38.     SwingSet swing;
  39.     JProgressBar progressBar;
  40.     JTextArea progressTextArea;
  41.     JButton loadButton;
  42.     JButton stopButton;
  43.     Timer timer;
  44.     Object  lock = new Object();
  45.  
  46.     public ProgressPanel(SwingSet swing) {
  47.     this.swing = swing;
  48.  
  49.     setLayout(new BorderLayout());
  50.  
  51.     JPanel textWrapper = new JPanel(new BorderLayout());
  52.     textWrapper.setBorder(swing.loweredBorder);
  53.     textWrapper.setAlignmentX(LEFT_ALIGNMENT);
  54.     progressTextArea = new MyTextArea();
  55.     progressTextArea.getAccessibleContext().setAccessibleName("Text progressively being loaded in");
  56.     progressTextArea.getAccessibleContext().setAccessibleDescription("This JTextArea is being filled with text from a buffer progressively a character at a time while the progress bar a the bottom of the window shows the loading progress");
  57.     textWrapper.add(progressTextArea, BorderLayout.CENTER);
  58.  
  59.     add(textWrapper, BorderLayout.CENTER);
  60.  
  61.     JPanel progressPanel = new JPanel();
  62.     add(progressPanel, BorderLayout.SOUTH);
  63.  
  64.     progressBar = new JProgressBar();
  65.     progressBar.setOrientation(JProgressBar.HORIZONTAL);
  66.     progressBar = new JProgressBar() {
  67.         public Dimension getPreferredSize() {
  68.         return new Dimension(300, super.getPreferredSize().height);
  69.         }
  70.     };
  71.     progressBar.getAccessibleContext().setAccessibleName("Text loading progress");
  72.     progressPanel.add(progressBar);
  73.     progressBar.setValue(0);
  74.     progressBar.setMinimum(0);
  75.     progressBar.setMaximum(text.length());
  76.  
  77.     loadButton = new JButton("Start Loading Text");
  78.     loadButton.addActionListener(new ActionListener() {
  79.         public void actionPerformed(ActionEvent e) {
  80.         startLoading();
  81.         }
  82.     });
  83.     progressPanel.add(loadButton);
  84.  
  85.         stopButton = new JButton("Stop Loading Text");
  86.         stopButton.addActionListener(new ActionListener() {
  87.             public void actionPerformed(ActionEvent e) {
  88.                 stopLoading();
  89.             }
  90.         });
  91.         stopButton.setEnabled(false);
  92.     progressPanel.add(stopButton);
  93.     }
  94.  
  95.     public Insets getInsets() {
  96.     return new Insets(10,10,10,10);
  97.     }
  98.  
  99.     public void startLoading() {
  100.         if(timer == null) {
  101.             loadButton.setEnabled(false);
  102.             stopButton.setEnabled(true);
  103.         timer = new Timer(25, this);
  104.         timer.start();
  105.         }
  106.     }
  107.  
  108.     public void stopLoading() {
  109.     if(timer != null) {
  110.        timer.stop();
  111.        timer = null;
  112.     }
  113.         loadButton.setEnabled(true);
  114.         stopButton.setEnabled(false);
  115.     }
  116.  
  117.     int textLocation = 0;
  118.  
  119.     String text =
  120.       "      The saying goes: if an infinite number of monkeys\n" +
  121.          "   typed on an infinite number of typewriters, eventually\n" +
  122.          "   all the great works of mankind would emerge. \n\n " +
  123.       "      Now, with today's high speed computers, we can\n " +
  124.          "   finally test the theory...\n\n" +
  125.       "      Lzskd jfy 92y;ho4 th;qlh sd 6yty;q2 hnlj 8sdf. Djfy\n " +
  126.          "   92y;ho4, th;qxhz d7yty; Q0hnlj 23&^ (# ljask djf y92y;h\n " +
  127.          "   fy92y; Sd6y ty;q2h nl jk la gfa harvin garvil lasdfsd\n " +
  128.          "   a83sl la8z 2 be or not to be... that is the question. Hath\n" +
  129.      "   forth not to want a banana, or to be a banana. Banana, I \n" +
  130.      "   knew him banana. Banana banana banana.\n\n" +
  131.          "          Well... it seemed like a good idea...\n\n\n\n\n\n\n\n\n\n" +
  132.       "             Hi Ewan and Montana!";
  133.  
  134.     public void actionPerformed (ActionEvent e) {
  135.         if(progressBar.getValue() < progressBar.getMaximum()) {
  136.         progressBar.setValue(progressBar.getValue() + 1);
  137.         progressTextArea.append(text.substring(textLocation, textLocation+1));
  138.         textLocation++;
  139.         } else {
  140.             stopLoading();
  141.             }
  142.     }
  143.  
  144.     class MyTextArea extends JTextArea {
  145.         public MyTextArea() {
  146.             super(null, 0, 0);
  147.         setEditable(false);
  148.         setText("");
  149.         }
  150.  
  151.         public float getAlignmentX () {
  152.             return LEFT_ALIGNMENT;
  153.         }
  154.  
  155.         public float getAlignmentY () {
  156.             return TOP_ALIGNMENT;
  157.         }
  158.     }
  159. }
  160.